package com.cx.base.day18_jdk8新特性;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @Author: robert.song
 * @Date: Created in 2022/6/14
 */
public class MethodRef {
    public static void main(String[] args) {
        way3();
    }

    //  对象名::方法名
    public static void way1() {
        String str = "对象名::方法名";
//        使用lamda实现匿名函数
        Supplier<String> supplier = () -> {
            return str;
        };
        System.out.println(supplier.get());
//        通过方法引用再简化
        Supplier<String> supplier1 = str::toString;
        System.out.println(supplier1.get());
    }

    //    类名::静态方法名
    public static void way2() {
        Function<Integer, String> function = Integer::toHexString;
        System.out.println(function.apply(10));
        Supplier<Long> supplier = System::currentTimeMillis;
        System.out.println(supplier.get());
    }

    //  类名::成员方法
    public static void way3() {
//        将参数传入，然后做方法参数
        Function<String, Integer> function = String::length;
        System.out.println(function.apply("10"));
        Function<Integer, String> function1 = Integer::toHexString;
        System.out.println(function1.apply(10));
//        将对象传入，然后操作对象方法
        Function<Integer, Integer> function2 = Integer::intValue;
        System.out.println(function2.apply(new Integer(10)));
    }

    //    类名::构造器
    public static void way4() {
        Supplier<String> supplier = String::new;
        System.out.println(supplier.get());
        Function<String, String> function = String::new;
        System.out.println(function.apply("类名::构造器"));
    }

    //    数组::构造器
    public static void way5() {
        Function<Integer, String[]> function = (length) -> {
            return new String[length];
        };
        System.out.println(function.apply(10));
        Function<Integer, String[]> function1 = String[]::new;
        System.out.println(function1.apply(10));
    }
}


